import request from '../utils/request';

export function userList(params) {

  const filters = params.filters?params.filters:[];
  let url = `api/auth/v1.0/user/list?page=${params.page}&per_page=${params.results}&q={"filters":${JSON.stringify(filters)},"order_by":[{"field":"${params.sortField?params.sortField:'id'}","direction":"${params.sortOrder?params.sortOrder:'desc'}"}]}`;
  return request(url);
}

export function search(params) {
 
  let url = `api/auth/v1.0/user/list?page=${params.page}&per_page=${params.results}&q={"filters":${JSON.stringify(params.filters)},"order_by":[{"field":"id","direction":"asc"}]}`;
  
  return request(url);
}

export function userTypeList() {
  return request(`api/auth/v1.0/user_type/list`);
}

export function roleList() {
  return request(`api/auth/v1.0/role/list`);
}

export async function update(params) {
  
  return request(`api/auth/v1.0/user?id=${params.id}`, {
    method: "PUT",
    body: {
      ...params,
    },
  });
}

export async function frozen(params) {
  return request(`api/auth/v1.0/user/frozen`, {
    method: "POST",
    body: {
      ...params,
    },
  });
}

export async function unfrozen(params) {
  return request(`api/auth/v1.0/user/unfrozen`, {
    method: "POST",
    body: {
      ...params,
    },
  });
}

export async function addRole(params) {
  return request(`api/auth/v1.0/user/role?id=${params.id}`, {
    method: "POST",
    body: {
      ...params,
    },
  });
}

export async function delRole(params) {
  return request(`api/auth/v1.0/user/role?id=${params.id}&role_list=${params.role_list}`, {
    method: "DELETE",
    body: {
      ...params,
    },
  });
}

export function userMe(params) {
  return request(`api/auth/v1.0/user/me`);
}